/***************************************************************************
| File: alarmsystem.c  -  Programa principal
|
| Autor: Joao Carlos Bittencourt, Vinicius Bittencourt, Italo Santos (IST)
| Data:  Maio 2012
***************************************************************************/
#define NT 4
#define NSTACK 4096

#include <stdio.h>
#include <cyg/io/io.h>
#include <cyg/kernel/kapi.h>

int sig_serial = 0; /* enable and disable serial functions */
int debug = 1; /* enable and disable debug */

Cyg_ErrNo err; /* declare the error handler */
cyg_io_handle_t serH; /* serial port handler */

/* declare of mail boxes for sending and receive
   date from UART */
cyg_handle_t mbx1H, mbx2H, mbx3H;
cyg_mbox mbx1, mbx2, mbx3;

/* now declare (and allocate space for) some kernel objects,
   like the two threads we will use */
cyg_thread thread_s[NT];		/* space for two thread objects */

char stack[NT][NSTACK];		/* space for two 4K stacks */

/* now the handles for the threads */
cyg_handle_t monitor_t, serial_read_t, serial_write_t;

/* and now variables for the procedure which is the thread */
cyg_thread_entry_t monitor, serial_read, serial_write;

/* and now a mutex to protect calls to the C library */
cyg_mutex_t seriallock;

/* we install our own startup routine which sets up threads */
void cyg_user_start(void)
{
  cyg_mutex_init(&seriallock);

  cyg_mbox_create( &mbx1H, &mbx1);
  cyg_mbox_create( &mbx2H, &mbx2);
  cyg_mbox_create( &mbx3H, &mbx3);

  cyg_thread_create(4, monitor, (cyg_addrword_t) 0,
		    "Interface Usuario", (void *) stack[0], NSTACK,
		    &monitor_t, &thread_s[0]);

  cyg_thread_create(3, serial_read, (cyg_addrword_t) 1,
		    "Read UART", (void *) stack[1], NSTACK,
		    &serial_read_t, &thread_s[1]); 

  cyg_thread_create(3, serial_write, (cyg_addrword_t) 2,
		    "Write UART", (void *) stack[2], NSTACK,
		    &serial_write_t, &thread_s[2]); 

  cyg_thread_resume(monitor_t);
  cyg_thread_resume(serial_read_t);
  cyg_thread_resume(serial_write_t);
}
